package com.joyer.utils;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 *
 * @author Joyer
 * @date 2014/12/3
 */
@Component
public class MessageUtil {
    private final Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
    @Autowired
    private MessageSource messageSource;

    /**
     * 取message属性文件，捕捉异常
     * @param key exception:key
     * @param objects exception:value
     * @return exception:message
     */
    public String getMessage(String key, Object[] objects) {
        String result;
        try {
            result = messageSource.getMessage(key, objects, Locale.CHINESE);
        } catch (NoSuchMessageException e) {
            logger.warn("获取值失败，对应key: " + key, e);
            result = "";
        }
        return result;
    }
}
